﻿namespace Huayou.Utils
{
    using Huayou.Common;
    using Huayou.UI.WebControls;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class DataGridUtils
    {
        public static string[] getID(DataGrid dgList)
        {
            return getID(dgList, "chkID");
        }

        public static string[] getID(DataGrid dgList, string chkID)
        {
            return getID(dgList, chkID, 0);
        }

        public static string[] getID(DataGrid dgList, string chkID, int index)
        {
            IList list = new ArrayList();
            foreach (DataGridItem item in dgList.Items)
            {
                Control control = item.Cells[0].FindControl(chkID);
                if ((control != null) && ((CheckBox) control).Checked)
                {
                    list.Add(item.Cells[index].Text);
                }
            }
            if (list.Count == 0)
            {
                return null;
            }
            string[] array = new string[list.Count];
            list.CopyTo(array, 0);
            return array;
        }

        public static void SetPage(DataGrid dg, IList list)
        {
            SetPage(dg, list.Count);
            dg.DataSource = list;
        }

        public static void SetPage(DataGrid dg, DataTable dt)
        {
            SetPage(dg, dt.Rows.Count);
            dg.DataSource = dt;
        }

        private static void SetPage(DataGrid dg, int recordnum)
        {
            int num = ((recordnum + dg.PageSize) - 1) / dg.PageSize;
            if (num == 0)
            {
                num = 1;
            }
            if (dg.CurrentPageIndex >= num)
            {
                dg.CurrentPageIndex = num - 1;
            }
            if (dg.GetType().FullName.Equals("Huayou.Common.DataGridTable"))
            {
                ((Huayou.Common.DataGridTable) dg).MaxRecord = recordnum;
            }
            else if (dg.GetType().FullName.Equals("Huayou.UI.WebControls.DataGridTable"))
            {
                ((Huayou.UI.WebControls.DataGridTable) dg).MaxRecord = recordnum;
            }
        }
    }
}

